package com.zl.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.system.comm.util.CheckUtil;
import com.system.comm.util.JsonUtils;
import com.system.common.mapper.SysParamMapper;
import com.system.common.pojo.BisBeautyConfig;
import com.system.common.pojo.BisCustomUser;
import com.system.common.pojo.BisShop;
import com.system.common.pojo.SysParamExample;
import com.zl.service.ZlBisShopService;

@Controller
@RequestMapping("order")
public class wechatOrderController {
	
	@Autowired
	private ZlBisShopService zlBisShopService;
	@Autowired
	private SysParamMapper sysParamMapper;
	
	@RequestMapping("listBeautyInfo")
	public String listBeautyInfo(String shop_id,HttpSession session,Model model){
		List<BisBeautyConfig> bbfList=zlBisShopService.queryBeautyConfig();
		SysParamExample example=new SysParamExample();
		com.system.common.pojo.SysParamExample.Criteria criteria=example.createCriteria();
		criteria.andParamKeyEqualTo("exchange_beauty");
		
		String exchange_beauty_str=sysParamMapper.selectByExample(example).get(0).getParamValue();
		int  exchange_beauty=Integer.parseInt(exchange_beauty_str);
		List<Map<String, Object>> mapList=new ArrayList<>();
		
		for (BisBeautyConfig config : bbfList) {
			Map<String, Object> map=new HashMap<>();
			
	    	int  buy_num=config.getBuyNum();
	    	map.put("configId", config.getConfigId());
	    	map.put("buyNum", buy_num);
	    	map.put("giveNum", config.getGiveNum());
	    	BigDecimal b1 = new BigDecimal(Double.toString(buy_num));  
	        BigDecimal b2 = new BigDecimal(Double.toString(exchange_beauty));  
	        double total_price=b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP).doubleValue();  
	        map.put("totalPrice", total_price);
	        mapList.add(map);
		}
		
		model.addAttribute("beautyList", mapList);
		model.addAttribute("shop_id", shop_id);
		
		return "wechat/my/buyBeauty";
	}
	
	@RequestMapping("showSubscribeTime")
	public void showSubscribeTime(HttpServletResponse response,String shopId){
		BisShop shops=zlBisShopService.selectByPrimaryKey(shopId);
		int beginTime=Integer.parseInt(shops.getBeginTime().substring(0, 2));
		int endTime=Integer.parseInt(shops.getEndTime().substring(0, 2));
		List<Integer> list=new ArrayList<>();
		for (int i = beginTime; i < endTime; i++) {
			list.add(i);
		}
		String shop=JsonUtils.toJson(list);
		CheckUtil.responseW(response, shop);
	}
	
	@RequestMapping("goMyOrder")
	public String goMyOrder(Model model,String index){
		model.addAttribute("index", index);
		return "wechat/my/order/myOrder";
	}

	@RequestMapping("goPayOrder")
	public void goPayOrder(Model model,HttpServletRequest request,HttpSession session,HttpServletResponse response){
		BisCustomUser user=CheckUtil.getUserSession(session);
        Map<String,Object> map=zlBisShopService.payOrder(user,request);
        String str=JsonUtils.toJson(map);
        CheckUtil.responseW(response,str);
	}
	
	
	
	@RequestMapping("saveBuyBeautyOrder")
	public void saveBuyBeautyOrder(HttpServletRequest request, HttpServletResponse response,HttpSession session){
		BisCustomUser bisCustomUser=CheckUtil.getUserSession(session);
		
		
		Map<String, Object> map=zlBisShopService.saveBuyBeautyOrder(request, bisCustomUser);
		String str=JsonUtils.toJson(map);
		CheckUtil.responseW(response, str);
		
	}
	
	@RequestMapping("listNurseOrder")
	public void listNurseOrder(HttpServletRequest request, HttpServletResponse response,HttpSession session){
		BisCustomUser bisCustomUser=CheckUtil.getUserSession(session);
		List<Map<String, Object>> map=zlBisShopService.orderProject(bisCustomUser);
		String str=JsonUtils.toJson(map);
		
		CheckUtil.responseW(response, str);
		
	}

	/**
	 * 取消订单
	 * @param request
	 * @param response 响应客户端
	 */
	@RequestMapping("cancelOrder")
	public void  cancelOrder(HttpServletRequest request,HttpServletResponse response){
		String order_id=request.getParameter("order_id");
		Map<String,Object> map=zlBisShopService.deleteOderId(order_id);
		String str=JsonUtils.toJson(map);
		CheckUtil.responseW(response,str);
	}

}
